export const navTo = (url = '', query = {}, type = 'navigate') => {
	if (!url && type != 'navigateBack') {
		uni.showToast({
			title: '页面路径不能为空',
			icon: 'none'
		})
		return
	}
	let result = [], params = ''
	if (!!query && Object.keys(query) != false) {
		for (const key in query) {
			const item = query[key]
			result.push(`${key}=${item}`)
		}
		params = result.join('&')
	}
	
	const path = result != false ? `${url}?${params}` : url
	switch (type) {
		case 'navigate':
			uni.navigateTo({
				url: path
			})
			break;
		case 'redirect':
			uni.redirectTo({
				url: path
			})
			break;
		case 'switchTab':
			uni.switchTab({
				url
			})
			break;
		case 'reLaunch':
			uni.reLaunch({
				url: path
			})
			break;
		case 'navigateBack':
			uni.navigateBack({
				delta: 1
			})
			break;
	}
}
// 正则表达式匹配11位手机号格式（不包含区号）
function validatePhoneNumber(phone) {
	const reg = /^1(?:3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-9])\d{8}$/
	return reg.test(phone)
}
// 校检身份证号码
function validateIDCard(id) {
	const reg = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]/
	return reg.test(id)
}

// 防抖函数
function debounce(func, wait = 1000, immediate = true) {
	let timer;
	// console.log(1);
	return function() {
		// console.log(123);
		let context = this,
			args = arguments;
		if (timer) clearTimeout(timer);
		if (immediate) {
			let callNow = !timer;
			timer = setTimeout(() => {
				timer = null;
			}, wait);
			if (callNow) func.apply(context, args);
		} else {
			timer = setTimeout(() => {
				func.apply(context, args);
			}, wait)
		}
	}
}
// 微信分享参数
 const getIndexShareObject = (obj, parentPath = '/pages/index/index') => {
  const rs = {
    ...obj,
  }
  const q = obj.path.split('?')
  rs.path = `${parentPath}?_goto=${q[0]}&${q[1]}`
  // rs.path = '/pages/index/index?_goto=' + q[0] + '&' + q[1]
  return rs
}
// 获取当前日期
const getCurrentDate = () => {
	const date = new Date()
	const year = date.getFullYear(),
		month = date.getMonth() + 1,
		day = date.getDate()
	const { osName } = uni.getDeviceInfo()
	// 在部分ios下无法格式化YY-mm-dd格式的日期
	if (osName == 'ios') {
		// return `${year}/${month>9?month:'0'+month}/${day>9?day:'0'+day} 00:00:00`
		return `${year}/${String(month).padStart(2, '0')}/${String(day).padStart(2, '0')} 00:00:00`
	} else {
		// return `${year}-${month>9?month:'0'+month}-${day>9?day:'0'+day} 00:00:00`
		return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')} 00:00:00`
	}
}

// 日期格式修改
const dateFormatModify = (date) => {
	const { osName } = uni.getDeviceInfo()
	// 在部分ios下无法格式化YY-mm-dd格式的日期
	if (osName == 'ios') {
		// return date.replace(/-/g, '/')
		return date.replaceAll('-', '/')
	}
	return date
}


export {
	validatePhoneNumber,
	debounce,
	getIndexShareObject,
	getCurrentDate,
	dateFormatModify,
	validateIDCard,
}